package vn.sunnet.backviet01.game.coloa.util;

import java.util.Arrays;

import org.anddev.andengine.entity.sprite.AnimatedSprite;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
import org.anddev.andengine.util.MathUtils;
import org.anddev.andengine.util.constants.TimeConstants;

import android.util.Log;

public class MyAnimatedSprite extends AnimatedSprite{

	public MyAnimatedSprite(float pX, float pY,
			TiledTextureRegion pTiledTextureRegion) {
		super(pX, pY, pTiledTextureRegion);
	}
	
//	@Override
//	protected void onManagedUpdate(final float pSecondsElapsed) {
//		super.onManagedUpdate(pSecondsElapsed);
//		if (this.mAnimationRunning) {
//			final long nanoSecondsElapsed = (long) (pSecondsElapsed * TimeConstants.NANOSECONDSPERSECOND);
//			this.mAnimationProgress += nanoSecondsElapsed;
//
//			if (this.mAnimationProgress > this.mAnimationDuration) {
//				this.mAnimationProgress %= this.mAnimationDuration;
//				if (this.mInitialLoopCount != AnimatedSprite.LOOP_CONTINUOUS) {
//					this.mLoopCount--;
//				}
//			}
//
//			if (this.mInitialLoopCount == AnimatedSprite.LOOP_CONTINUOUS
//					|| this.mLoopCount >= 0) {
//				final int currentFrameIndex = this.calculateCurrentFrameIndex();
//
//				if (this.mFrames == null) {
//					this.setCurrentTileIndex(this.mFirstTileIndex
//							+ currentFrameIndex);
//				} else {
//					this.setCurrentTileIndex(this.mFrames[currentFrameIndex]);
//				}
//			} else {
//				this.mAnimationRunning = false;
//				if (this.mAnimationListener != null) {
//					this.mAnimationListener.onAnimationEnd(this);
//				}
//			}
//		}
//	}
//	
//	private int calculateCurrentFrameIndex() {
//		final long animationProgress = this.mAnimationProgress;
//		final long[] frameEnds = this.mFrameEndsInNanoseconds;
//		final int frameCount = this.mFrameCount;
//		for (int i = 0; i < frameCount; i++) {
//			if (frameEnds[i] > animationProgress) {
//				return i;
//			}
//		}
//
//		return frameCount - 1;
//	}
//	
//	@SuppressWarnings("unused")
//	private AnimatedSprite init(final long[] pFrameDurations,
//			final int frameCount, final int[] pFrames,
//			final int pFirstTileIndex, final int pLoopCount,
//			final IAnimationListener pAnimationListener) {
//		this.mFrameCount = frameCount;
//		this.mAnimationListener = pAnimationListener;
//		this.mInitialLoopCount = pLoopCount;
//		this.mLoopCount = pLoopCount;
//		this.mFrames = pFrames;
//		this.mFirstTileIndex = pFirstTileIndex;
//
//		if (this.mFrameEndsInNanoseconds == null
//				|| this.mFrameCount > this.mFrameEndsInNanoseconds.length) {
//			this.mFrameEndsInNanoseconds = new long[this.mFrameCount];
//		}
//
//		final long[] frameEndsInNanoseconds = this.mFrameEndsInNanoseconds;
//		MathUtils.arraySumInto(pFrameDurations, frameEndsInNanoseconds,
//				TimeConstants.NANOSECONDSPERMILLISECOND);
//
//		final long lastFrameEnd = frameEndsInNanoseconds[this.mFrameCount - 1];
//		this.mAnimationDuration = lastFrameEnd;
//		this.mAnimationProgress = 0;
//		this.mAnimationRunning = true;
//		return this;
//	}
//
//	// ===========================================================
//	// Inner and Anonymous Classes
//	// ===========================================================
	
	public void resetAnimatedSprite(final float factor) {
		final long[] frameEndsInNanoseconds = this.mFrameEndsInNanoseconds;

		resetArray(factor, frameEndsInNanoseconds);

		final long lastFrameEnd = frameEndsInNanoseconds[this.mFrameCount - 1];
		this.mAnimationDuration = lastFrameEnd;
		mAnimationProgress *= factor;
		
		this.mAnimationRunning = true;
		
	}
	
	public static final void resetArray(final float factor, final long...pFrameDurations ) {
		int size = pFrameDurations.length;
		
		for (int i = 0; i < size; i++) {
			pFrameDurations[i] = (long)(factor * pFrameDurations[i]);
		}
		
	}
	
	public void resetAnimation(long... pFrameDurations) {
		long animationProgress = 0;
		long redundance = 0;
		int i = getCurrentTileIndex() - mFirstTileIndex;
		if (i >= mFrameCount)
			i = mFrameCount - 1;
		
		try {
			animationProgress = mFrameEndsInNanoseconds[i];
		} catch (Exception e) {
			animationProgress = mFrameEndsInNanoseconds[mFrameCount - 1];
		}
		
		redundance = mAnimationDuration % animationProgress;
		
		final long[] frameEndsInNanoseconds = this.mFrameEndsInNanoseconds;

		MathUtils.arraySumInto(pFrameDurations, frameEndsInNanoseconds,
				TimeConstants.NANOSECONDSPERMILLISECOND);

		final long lastFrameEnd = frameEndsInNanoseconds[this.mFrameCount - 1];
		this.mAnimationDuration = lastFrameEnd;
		try {
			mAnimationProgress = frameEndsInNanoseconds[i] + redundance;
		} catch (Exception e) {
			Log.e("i = " + i +  " mFrameCount = "+mFrameCount ,"mAnimationProgress = "+mAnimationProgress);
			mAnimationProgress = frameEndsInNanoseconds[frameEndsInNanoseconds.length - 1] + redundance;
		}

		this.mAnimationRunning = true;

	}

	public void resetAnimation(long pFrameDurationEach) {
		long animationProgress = 0;
		long redundance = 0;
		int i = getCurrentTileIndex() - mFirstTileIndex;
		if (i >= mFrameCount)
			i = mFrameCount - 1;
		
		try {
			animationProgress = mFrameEndsInNanoseconds[i];
		} catch (Exception e) {
			animationProgress = mFrameEndsInNanoseconds[mFrameCount - 1];
		}
		
		redundance = mAnimationDuration % animationProgress;
		
		final long[] frameDurations = new long[mFrameEndsInNanoseconds.length];
		Arrays.fill(frameDurations, pFrameDurationEach);

		final long[] frameEndsInNanoseconds = this.mFrameEndsInNanoseconds;
		MathUtils.arraySumInto(frameDurations, frameEndsInNanoseconds,
				TimeConstants.NANOSECONDSPERMILLISECOND);

		final long lastFrameEnd = frameEndsInNanoseconds[this.mFrameCount - 1];
		this.mAnimationDuration = lastFrameEnd;
		if (mAnimationProgress != 0) {
			try {
				mAnimationProgress = frameEndsInNanoseconds[i] + redundance;
			} catch (Exception e) {
				Log.e("i = " + i +  " mFrameCount = "+mFrameCount ,"mAnimationProgress = "+mAnimationProgress);
				mAnimationProgress = frameEndsInNanoseconds[frameEndsInNanoseconds.length - 1] + redundance;
			}
		} else
			this.mAnimationProgress = 0;
		this.mAnimationRunning = true;
	}
}
